package com.jsb.laundry.common.mq;

import com.jsb.laundry.common.enums.OrderStatus;
import com.jsb.laundry.dao.OrderDao;
import com.jsb.laundry.model.entity.JsbOrders;
import com.jsb.laundry.model.vo.PrePayVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 30分钟订单为支付时消费者
 */
@Slf4j
@Component
public class OrderCancelConsumer {

    @Resource
    private OrderDao orderDao;

    /**
     * 30分钟未支付队列消费者
     */
    @RabbitListener(queues = WaitPayConfig.ORDER_30_CANCEL_QUEUE)
    public void receiveOrderCancelMessage(Message message, PrePayVo prePayVo) {
        log.info("接受到队列{}的消息:{}", message.getMessageProperties().getConsumerQueue(), prePayVo);
        String orderId = prePayVo.getOrderId();
        String userId = prePayVo.getUserId();
        //订单未支付，调整订单为已取消 (未支付、未删除)
        JsbOrders orderInfo = orderDao.getOrderInfo(orderId);

        //通过订单id 查询订单信息jsb_orders  未支付的订单取消
        if (orderInfo!=null && OrderStatus.WAIT_PAY.getCode().equals(orderInfo.getOrderStatus()) && "0".equals(orderInfo.getDelFlag())) {
            JsbOrders order = new JsbOrders();
            order.setUserId(userId).setOrderId(orderId).setOrderStatus(OrderStatus.SYSTEM_CANCEL_ORDER_PAY.getCode())
                    .setCancelReason("30分钟未支付,系统自动取消订单").setCancelType("system");
            int var0 = orderDao.changeOrderStatus(order);
        }

    }

}
